//
//  ViewController.m
//  ScrollConnectDemo
//
//  Created by Glen Eller on 2017/11/20.
//  Copyright © 2017年 none. All rights reserved.
//

#import "ViewController.h"

#import "HorizonScrollView.h"
#import "HorizonScrollSubView.h"

//#import "HorizonModel.h"

@interface ViewController ()

//父视图scrollview——>scrollview装子视图scrollview
@property (nonatomic, strong) UIScrollView *fatScrollView;

@property (nonatomic, strong) NSArray *totalData;
//存放 第一行 横着的数据
@property (nonatomic, strong) NSArray *bigDataArray;

//存放当前滚动视图位置
@property (nonatomic, strong) NSMutableArray *pageNoArray;

@end

@implementation ViewController

- (NSMutableArray *)pageNoArray
{
    if (!_pageNoArray) {
        _pageNoArray = [NSMutableArray array];
    }
    return _pageNoArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1.0];
    
    //添加视图
    [self.view addSubview:self.fatScrollView];
    
    //请求数据
    [self requestListData];
    
}

- (void)requestListData
{
    self.totalData = @[
                       @{@"detail_id":@"111",
                         @"title":@"1",
                         @"levelNum":@"1",
                         @"childrens":@[
                                 @{@"title":@"1.1",
                                   @"levelNum":@"2",
                                   @"childrens":@[
                                           @{@"title":@"1.1.1",
                                             @"levelNum":@"3",
                                             @"childrens":@[
                                                     @{@"title":@"1.1.1.1",
                                                       @"levelNum":@"4",
                                                       @"childrens":@[
                                                               @{@"title":@"1.1.1.1.1"}
                                                               ],
                                                       },
                                                     @{@"title":@"1.1.1.2",
                                                       @"levelNum":@"4",
                                                       @"childrens":@[
                                                               @{@"title":@"1.1.1.1.2"}
                                                               ]
                                                       }
                                                     ],
                                             },
                                           ]
                                   },
                                 @{@"title":@"1.2",
                                   @"levelNum":@"2",
                                   @"childrens":@[
                                           @{@"title":@"1.2.1",
                                             @"levelNum":@"3",
                                             @"childrens":@[
                                                     @{@"title":@"1.2.1.1",
                                                       @"levelNum":@"4",
                                                       @"childrens":@[
                                                               @{@"title":@"1.2.1.1.1"}
                                                               ],
                                                       },
                                                     @{@"title":@"1.2.1.2",
                                                       @"levelNum":@"4",
                                                       @"childrens":@[
                                                               @{@"title":@"1.2.1.1.2"}
                                                               ]
                                                       }
                                                     ],
                                             }]
                                   }]
                         },
                       @{@"detail_id":@"222",
                         @"title":@"2",
                         @"levelNum":@"1",
                         @"childrens":@[
                                 @{@"title":@"2.1",
                                   @"levelNum":@"2",
                                   @"childrens":@[
                                           @{@"title":@"2.1.1",
                                             @"levelNum":@"3",
                                             @"childrens":@[
                                                     @{@"title":@"2.1.1.1",
                                                       @"levelNum":@"4",
                                                       @"childrens":@[
                                                               @{@"title":@"1.1.1.1.1"}
                                                               ],
                                                       },
                                                     @{@"title":@"1.1.1.2",
                                                       @"levelNum":@"4",
                                                       @"childrens":@[
                                                               @{@"title":@"1.1.1.1.2"}
                                                               ]
                                                       }
                                                     ],
                                             },
                                           ]
                                   },
                                 @{@"title":@"2.2",
                                   @"levelNum":@"2",
                                   @"childrens":@[
                                           @{@"title":@"2.2.1",
                                             @"levelNum":@"3",
                                             @"childrens":@[
                                                     @{@"title":@"2.2.1.1",
                                                       @"levelNum":@"4",
                                                       @"childrens":@[
                                                               @{@"title":@"2.2.1.1.1"}
                                                               ],
                                                       },
                                                     @{@"title":@"2.2.1.2",
                                                       @"levelNum":@"4",
                                                       @"childrens":@[
                                                               @{@"title":@"2.2.1.1.2"}
                                                               ]
                                                       }
                                                     ],
                                             }]
                                   }]
                         }];
    //动态创建视图
    [self reloadPageViewsWithData:self.totalData andRowNum:0];
}

//根据传值 刷新界面
- (void)reloadPageViewsWithData:(NSArray *)totalData andRowNum:(NSString *)levelNum
{
    if (!totalData || totalData.count == 0) {
        NSLog(@"没有值");
        return;
    }

    NSArray *lineArray = totalData;
    
    int i = 0;//记录行
    
    do {
        //创建第j行视图
        CGRect frame = CGRectMake(0, kHorizonScrollH*i, self.view.bounds.size.width, kHorizonScrollH);
        HorizonScrollView *lineView = [self generalCommonLineViewWithFrame:frame];
        lineView.tag = 500 + i;
        __weak typeof(self)weakSelf = self;
        lineView.ScrollingBlock = ^(NSString *levelNum, NSInteger pageNo) {
//            [weakSelf reloadPageViewsWithData:<#(NSArray *)#> andRowNum:<#(NSString *)#>];
        };
        lineView.datasArray = lineArray;
        [self.fatScrollView addSubview:lineView];
        
        CGSize originSize = self.fatScrollView.contentSize;
        originSize.height += kHorizonScrollH;
        self.fatScrollView.contentSize = originSize;
        
        for (NSDictionary *subDic in lineArray) {
            NSArray *allKeys = subDic.allKeys;
            if ([allKeys containsObject:@"childrens"]) {
                NSArray *childrens = subDic[@"childrens"];
                if (!childrens || childrens.count == 0) {
                    lineArray = nil;
                } else {
                    lineArray = childrens;
                }
                break;
            } else {
                lineArray = nil;
            }
        }
        i++;
        
    } while (lineArray && lineArray.count>0);
    
//
//    NSMutableArray *lineDatas = [NSMutableArray arrayWithArray:allValue];
//    int j = 0;
//    NSMutableArray *finalDatas = [NSMutableArray array];
//    do {
//        int i = 0;
//        //创建第j行视图
//        CGRect frame = CGRectMake(0, kHorizonScrollH*j, self.view.bounds.size.width, kHorizonScrollH);
//
//        HorizonScrollView *lineView = [self generalCommonLineViewWithFrame:frame];
//        lineView.tag = 500 + j;
//        lineView.datasArray = lineDatas;
//        [self.fatScrollView addSubview:lineView];
//
//        CGSize originSize = self.fatScrollView.contentSize;
//        originSize.height += kHorizonScrollH;
//        self.fatScrollView.contentSize = originSize;
//
//        [finalDatas removeAllObjects];
//        for (id object in lineDatas) {//第j+1行数据分析
//
//            if ([object isKindOfClass:[NSDictionary class]]) {
//                NSDictionary *element = object;
//
//                NSArray *subArray = element.allValues;
//                for (id subObject in subArray) {
//                    if ([subObject isKindOfClass:[NSDictionary class]]) {
//                        NSDictionary *subElement = subObject;
//                        [finalDatas addObject:subElement];
//                    }
//                }
//                break;
//            }
//            i++;
//        }
//        lineDatas = [finalDatas copy];
//        j++;
//    } while (lineDatas && lineDatas.count > 0);
    
}

- (HorizonScrollView *)generalCommonLineViewWithFrame:(CGRect)frame
{
    HorizonScrollView *view = [[HorizonScrollView alloc] initWithFrame:frame];
    return view;
}

- (UIScrollView *)fatScrollView
{
    if (!_fatScrollView) {
        CGRect frame = self.view.bounds;
        frame.origin.y = 64.f;
        frame.size.height -= 64.f;
        _fatScrollView = [[UIScrollView alloc] initWithFrame:frame];
        _fatScrollView.backgroundColor = [UIColor clearColor];
        
        _fatScrollView.showsVerticalScrollIndicator = NO;
        _fatScrollView.showsHorizontalScrollIndicator = NO;
        _fatScrollView.bounces = NO;
        
        _fatScrollView.contentOffset = CGPointMake(0, 0);
    }
    return _fatScrollView;
}

@end
